﻿(function ($)
{
    $.validator.addMethod(
            "notnull",
            function (value, element)
            {
                if (!value) return true;
                return !$(element).hasClass("l-text-field-null");
            },
            "不能为空"
    );

    //字母数字
    jQuery.validator.addMethod("alnum", function (value, element)
    {
        return this.optional(element) || /^[a-zA-Z0-9]+$/.test(value);
    }, "只能包括英文字母和数字");

    // 手机号码验证   
    jQuery.validator.addMethod("cellphone", function (value, element)
    {
        var length = value.length;
        return this.optional(element) || (length == 11 && /^(1\d{10})$/.test(value));
    }, "请正确填写手机号码");

    // 电话号码验证   
    jQuery.validator.addMethod("telephone", function (value, element)
    {
        var tel = /^(\d{3,4}-?)?\d{7,9}$/g;
        return this.optional(element) || (tel.test(value));
    }, "请正确填写电话号码");

    // 邮政编码验证
    jQuery.validator.addMethod("zipcode", function (value, element)
    {
        var tel = /^[0-9]{6}$/;
        return this.optional(element) || (tel.test(value));
    }, "请正确填写邮政编码");

    // 汉字
    jQuery.validator.addMethod("chcharacter", function (value, element)
    {
        var tel = /^[\u4e00-\u9fa5]+$/;
        return this.optional(element) || (tel.test(value));
    }, "请输入汉字");



    // QQ
    jQuery.validator.addMethod("qq", function (value, element)
    {
        var tel = /^[1-9][0-9]{4,}$/;
        return this.optional(element) || (tel.test(value));
    }, "请输入正确的QQ");

    // 用户名
    jQuery.validator.addMethod("username", function (value, element)
    {
        return this.optional(element) || /^[a-zA-Z][a-zA-Z0-9_]+$/.test(value);
    }, "用户名格式不正确");
    // 身份证号码
    jQuery.validator.addMethod("idcard", function (value, element)
    {
    	var city = {
    			11 : "北京",
    			12 : "天津",
    			13 : "河北",
    			14 : "山西",
    			15 : "内蒙古",
    			21 : "辽宁",
    			22 : "吉林",
    			23 : "黑龙江 ",
    			31 : "上海",
    			32 : "江苏",
    			33 : "浙江",
    			34 : "安徽",
    			35 : "福建",
    			36 : "江西",
    			37 : "山东",
    			41 : "河南",
    			42 : "湖北 ",
    			43 : "湖南",
    			44 : "广东",
    			45 : "广西",
    			46 : "海南",
    			50 : "重庆",
    			51 : "四川",
    			52 : "贵州",
    			53 : "云南",
    			54 : "西藏 ",
    			61 : "陕西",
    			62 : "甘肃",
    			63 : "青海",
    			64 : "宁夏",
    			65 : "新疆",
    			71 : "台湾",
    			81 : "香港",
    			82 : "澳门",
    			91 : "国外 "
    		};
	        var pass= true;
    		if(!value || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(value)){
    	        pass = false;
    	    }else if(!city[value.substr(0,2)]){
                pass = false;
            }else{
                //18位身份证需要验证最后一位校验位
                if(value.length == 18){
                	value = value.split('');
                    //∑(ai×Wi)(mod 11)
                    //加权因子
                    var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
                    //校验位
                    var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
                    var sum = 0;
                    var ai = 0;
                    var wi = 0;
                    for (var i = 0; i < 17; i++)
                    {
                        ai = value[i];
                        wi = factor[i];
                        sum += ai * wi;
                    }
                    var last = parity[sum % 11];
                    if(parity[sum % 11] != value[17]){
                        pass =false;
                    }
                }
            }
        return pass;
    }, "身份证号码有误");
    
    // 电话或手机号码
    jQuery.validator.addMethod("mobortel", function (value, element)
    {
    	var regTel1 = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;//带区号的固定电话
        var regTel2 = /^(\d{7,8})(-(\d{3,}))?$/;//不带区号的固定电话
    	var mobile = /^1[3|5|8]\d{9}$/ ;
        return this.optional(element) || mobile.test(value) || regTel1.test(value) || regTel2.test(value);
    }, "电话/手机格式不正确");
    
    // 统一社会信用代码
    jQuery.validator.addMethod("uniscreg", function (value, element)
    {
    	var unreg = /[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}/g;
        return this.optional(element) || (unreg.test(value));
    }, "统一社会信用代码格式不正确");

})(jQuery);